﻿using Cxuu.Common;
using Cxuu.Models;
using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using PetaPoco.Core;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Repository
{
    [UseDI(ServiceLifetime.Scoped)]
    public class AdminUserRepository : BaseRepository<AdminUser, PetaPocoContext>
    {

        public AdminUserRepository(PetaPocoContext petaPocoContext) : base(petaPocoContext) { }



        /// <summary>
        /// 联表查询用户组
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<Page<ViewAdminUser>> GetPageListAsync(int page, int limit)
        {
            Sql sql = Sql.Builder;
            sql.Select("A.Id,B.Groupname as Groupname,A.Gid,A.Username,A.Nickname,A.Logintime,A.Loginip,A.Status")
                .From("cxuu_admin_user as A")
                .LeftJoin("cxuu_admin_group as B")
                .On("A.Gid = B.Id");
            sql.OrderBy("A.Id desc");
            return await CreateDAO().PageAsync<ViewAdminUser>(page, limit, sql);
        }

        /// <summary>
        /// 查询指定列  ID  Name 
        /// </summary>
        /// <returns></returns>
        public async Task<List<UserIdAndName>> GetUserIdAndNameListAsync()
        {
            PocoData data = PocoData.ForType(typeof(AdminUser), new ConventionMapper());
            Sql sql = Sql.Builder;
            sql.Select("Id,Username,Status")
                .From(new object[] { data.TableInfo.TableName })
                .Where("Status = 1")
                .OrderBy("Id desc");
            return await CreateDAO().FetchAsync<UserIdAndName>(sql);
        }


        /// <summary>
        /// 判断当前用户名是否存在
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public async Task<bool> GetByUsernameAsync(string username)
        {
            PocoData data = PocoData.ForType(typeof(AdminUser), new ConventionMapper());
            Sql sql = Sql.Builder;
            sql.Select("*");
            sql.From(new object[] { data.TableInfo.TableName });
            sql.Where("Username =@0", username);

            return await CreateDAO().FirstOrDefaultAsync<AdminUser>(sql) == null;
        }


        /// <summary>
        /// 判断当前用户是否存在
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public async Task<bool> GetByGidAsync(int gid)
        {
            PocoData data = PocoData.ForType(typeof(AdminUser), new ConventionMapper());
            Sql sql = Sql.Builder;
            sql.Select("*");
            sql.From(new object[] { data.TableInfo.TableName });
            sql.Where("Gid =@0", gid);

            return await CreateDAO().FirstOrDefaultAsync<AdminUser>(sql) != null;
        }


        public async Task<AdminUser> CheckUorP(string username, string md5Password)
        {
            PocoData data = PocoData.ForType(typeof(AdminUser), new ConventionMapper());
            Sql sql = Sql.Builder;
            sql.Select("*");
            sql.From(new object[] { data.TableInfo.TableName });
            sql.Where("Username =@0 AND Password = @1", username, md5Password);

            return await CreateDAO().FirstOrDefaultAsync<AdminUser>(sql);
        }

        public virtual async Task<int> UpdateAsync(EditAdminUser user, int id)
        {
            Sql sql = Sql.Builder;
            if (!string.IsNullOrEmpty(user.Password))
            {
                sql.Append("SET Username = @0 ,Nickname = @1,Status=@2,Password= @3 ,Gid = @4", user.Username, user.Nickname, user.Status, user.Password, user.Gid)
                    .Where("Id =@0", id);
            }
            else
            {
                sql.Append("SET Username = @0 ,Nickname = @1,Status=@2,Gid = @3", user.Username, user.Nickname, user.Status, user.Gid)
                    .Where("Id =@0", id);
            }
            return await CreateDAO().UpdateAsync<AdminUser>(sql);
        }

        public virtual async Task<int> PassWordUpdateAsync(EditPassWord passWord, int id)
        {
            Sql sql = Sql.Builder;

            sql.Append("SET Nickname = @0,Password= @1", passWord.Nickname, passWord.Password)
                .Where("Id =@0", id);

            return await CreateDAO().UpdateAsync<AdminUser>(sql);
        }


        public virtual async Task<int> UpdateLoginInfoAsync(EditAdminUser admin_User, int primaryKeyValue)
        {
            Sql sql = Sql.Builder;
            sql.Append("SET Logintime=@0, Loginip=@1 WHERE Id=@2", admin_User.Logintime, admin_User.Loginip, primaryKeyValue);
            return await CreateDAO().UpdateAsync<AdminUser>(sql);
        }



    }
}
